package br.com.manutencaocarro.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import br.com.manutencaocarro.R;
import br.com.manutencaocarro.dao.CarroDAO;
import br.com.manutencaocarro.dao.LojaDAO;
import br.com.manutencaocarro.modelo.Carro;
import br.com.manutencaocarro.modelo.Loja;

public class ListagemCarrosActivity extends Activity 
{
	private ListView lista;
	private Carro carro;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_carros);

		lista = (ListView) findViewById(R.id.lista_carros);

		lista.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
				Carro carroClicado = (Carro) adapter.getItemAtPosition(position);

				Intent irParaFormulario = new Intent( ListagemCarrosActivity.this, CadastraCarroActivity.class );
				irParaFormulario.putExtra( getString(R.string.carroselecionado), carroClicado );
				startActivity(irParaFormulario);
			}
		});

		lista.setOnItemLongClickListener( new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
				carro = (Carro) adapter.getItemAtPosition(position);
				return false;
			}
		});

		registerForContextMenu(lista);
	}

	@Override
	protected void onResume() {
		super.onResume();
		carregaLista();
	}

	private void carregaLista() 
	{
		CarroDAO dao = new CarroDAO(this);
		List<Carro> carros = dao.getLista();
		dao.close();

		int layout = android.R.layout.simple_list_item_1;
		ArrayAdapter<Carro> adapter = new ArrayAdapter<>(this, layout , carros);

		lista.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
	{
		List<Loja> listLojas = new LojaDAO(this).getLista();

		MenuItem compraPeca = menu.add( R.string.cadastrar_compra_de_peca );
		if( listLojas.size() > 0 )
		{
			compraPeca.setOnMenuItemClickListener( new OnMenuItemClickListener() {
				@Override
				public boolean onMenuItemClick(MenuItem item) 
				{
					Intent irParaFormulario = new Intent( ListagemCarrosActivity.this, CadastraPecaActivity.class );
					Bundle params = new Bundle();
					params.putLong( "idCarro", carro.getId() );

					irParaFormulario.putExtras(params);
					startActivity(irParaFormulario);
					return false;
				}
			});
		} else {
			Toast.makeText( this, R.string.cadastro_peca, Toast.LENGTH_LONG ).show();
		}

		MenuItem listarPecas = menu.add( R.string.listar_pecas_compradas );
		listarPecas.setOnMenuItemClickListener( new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) 
			{
				Intent irParaFormulario = new Intent( ListagemCarrosActivity.this, ListagemPecasActivity.class );
				Bundle params = new Bundle();
				params.putLong( "idCarro", carro.getId() );
				irParaFormulario.putExtras(params);
				startActivity(irParaFormulario);
				return false;
			}
		});

		MenuItem excluir = menu.add( R.string.excluir );

		excluir.setOnMenuItemClickListener( new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				CarroDAO dao = new CarroDAO(ListagemCarrosActivity.this);
				dao.deleta(carro);
				dao.close();
				carregaLista();
				Toast.makeText( ListagemCarrosActivity.this, R.string.carro_excluido_com_sucesso, Toast.LENGTH_LONG ).show();

				return false;
			}
		});

		MenuItem detalhes = menu.add( R.string.ver_detalhes );

		detalhes.setOnMenuItemClickListener( new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent irParaFormulario = new Intent( ListagemCarrosActivity.this, CadastraCarroActivity.class );
				Bundle params = new Bundle();
				params.putSerializable( getString(R.string.carroselecionado), carro );
				params.putBoolean( getString(R.string.detalhar), true );
				irParaFormulario.putExtras(params);
				startActivity(irParaFormulario);

				return false;
			}
		});

		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listagem_carros, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		int itemClicado = item.getItemId();

		switch (itemClicado) 
		{
		case R.id.novo_carro :
			Intent irParaFormulario = new Intent(this, CadastraCarroActivity.class);

			startActivity(irParaFormulario);
			return false;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}